//
//  SEGMyProfileViewCell.m
//  ExcellentHome
//
//  Created by 刘山 on 16/11/19.
//  Copyright © 2016年 陈区. All rights reserved.
//

#import "SEGMyProfileViewCell.h"

@interface SEGMyProfileViewCell () <UITextFieldDelegate, UITextViewDelegate>

@end

@implementation SEGMyProfileViewCell
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self setupUI];
    }
    return self;
}
- (void)setupUI
{

    self.selectionStyle = UITableViewCellSelectionStyleNone;

    [self.contentView addSubview:self.titleLabel];
    [self.titleLabel setFont:[UIFont systemFontOfSize:16]];
    [self.titleLabel setTextColor:kColor_2];

    [self.contentView addSubview:self.ContentLabel];
    [self.ContentLabel setFont:[UIFont systemFontOfSize:12]];
    [self.ContentLabel setTextColor:kColor_3];
    [self.ContentLabel setHidden:YES];

    [self.contentView addSubview:self.contentTextView];
    [self.contentTextView setFont:[UIFont systemFontOfSize:16]];
    self.contentTextView.delegate = self;
    self.contentTextView.returnKeyType = UIReturnKeyDone;
    [self.contentTextView setTextColor:kColor_1];
    self.contentTextView.backgroundColor = self.contentTextView.superview.backgroundColor;

    [self.contentView addSubview:self.contentField];
    [self.contentField setFont:[UIFont systemFontOfSize:16]];
    self.contentField.delegate = self;
    [self.contentField setTextColor:kColor_1];
    self.contentField.returnKeyType = UIReturnKeyDone;

    [self.contentView addSubview:self.switchContent];
    [self.switchContent setHidden:YES];

    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker* make) {
        make.left.top.equalTo(self.contentView).mas_offset(15);
        // iOS10 以上的适配
        if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 10.0) {
            make.bottom.equalTo(self.contentView).mas_offset(-15);
        }
    }];

    //SAM:iOS10兼容-cell
    [self.contentView mas_remakeConstraints:^(MASConstraintMaker* make) {
        // iOS10 以上的适配
        if ([[[UIDevice currentDevice] systemVersion] floatValue] < 10.0) {
            make.left.top.right.equalTo(self);
            make.bottom.equalTo(self.titleLabel.mas_bottom).mas_offset(15);
        }else{
            make.edges.equalTo(self);
        }
        
    }];
}

- (void)setIsTextView:(BOOL)isTextView
{
    _isTextView = isTextView;

    [self.contentField setHidden:_isTextView];
    [self.contentTextView setHidden:!_isTextView];
    self.contentTextView.textContainer.lineFragmentPadding = 0;
    self.contentTextView.textContainerInset = UIEdgeInsetsZero;
    
    [self.switchContent setHidden:YES];

    if (_isTextView) {

        NSDictionary* attribute = @{ NSFontAttributeName : self.titleLabel.font };

        CGSize textSize = [@"测试" boundingRectWithSize:CGSizeMake(0.3786 * LYScreenWidth(), 0) options:NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:attribute context:nil].size;

        [self.contentTextView mas_makeConstraints:^(MASConstraintMaker* make) {
            make.top.equalTo(self.contentView).mas_offset(15);
            make.left.equalTo(self.contentView).mas_offset(97);
            make.right.equalTo(self.contentView).mas_offset(-15);
            make.height.mas_greaterThanOrEqualTo(textSize.height * 3).priorityHigh();
            // iOS10 以上的适配
            if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 10.0) {
                make.bottom.equalTo(self.contentView).mas_offset(-15);
            }
        }];

        //SAM:iOS10兼容-cell
        [self.contentView mas_makeConstraints:^(MASConstraintMaker* make) {
            // iOS10 以上的适配
            if ([[[UIDevice currentDevice] systemVersion] floatValue] < 10.0) {
                make.left.top.right.equalTo(self);
                make.bottom.equalTo(self.contentTextView.mas_bottom).mas_offset(15);
            }else{
                make.edges.equalTo(self);
            }
            
        }];
    }
    else {
        [self.contentField mas_remakeConstraints:^(MASConstraintMaker* make) {
            make.top.equalTo(self.contentView).mas_offset(15);
            make.left.equalTo(self.contentView).mas_offset(97);
            make.right.equalTo(self.contentView).mas_offset(-15);
            // iOS10 以上的适配
            if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 10.0) {
                make.bottom.equalTo(self.contentView).mas_offset(-15);
            }
        }];

        //SAM:iOS10兼容-cell
        [self.contentView mas_remakeConstraints:^(MASConstraintMaker* make) {
            // iOS10 以上的适配
            if ([[[UIDevice currentDevice] systemVersion] floatValue] < 10.0) {
                make.left.top.right.equalTo(self);
                make.bottom.equalTo(self.titleLabel.mas_bottom).mas_offset(15);
            }else{
                make.edges.equalTo(self);
            }
            
        }];
    }
}

- (BOOL)textView:(UITextView*)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString*)text
{
    if (self.indexPath.row == 0) {
    }
    return YES;
}

- (BOOL)textField:(UITextField*)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString*)string
{
    return YES;
}

- (BOOL)textFieldShouldBeginEditing:(UITextField*)textField
{
    if (self.indexPath.row == 1) {
        self.contentField.keyboardType = UIKeyboardTypePhonePad;
    }
    else {
        self.contentField.keyboardType = UIKeyboardTypeDefault;
    }

    return YES;
}
- (void)textFieldDidBeginEditing:(UITextField*)textField
{
}

- (void)textFieldDidEndEditing:(UITextField*)textField
{
}
- (void)setIsOrder:(BOOL)isOrder
{
    _isOrder = isOrder;
    [self.contentField setHidden:YES];
    [self.contentTextView setHidden:YES];
    [self.switchContent setHidden:!_isOrder];
    [self.ContentLabel setHidden:!_isOrder];

    if (_isOrder) {

        [self.titleLabel mas_remakeConstraints:^(MASConstraintMaker* make) {
            make.left.top.equalTo(self.contentView).mas_offset(15);
        }];

        [self.ContentLabel mas_remakeConstraints:^(MASConstraintMaker* make) {
            make.top.equalTo(self.titleLabel.mas_bottom).mas_offset(10);
            make.left.equalTo(self.contentView).mas_offset(15);
            // iOS10 以上的适配
            if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 10.0) {
                make.bottom.equalTo(self.contentView).mas_offset(-15);
            }
        }];

        [self.switchContent mas_remakeConstraints:^(MASConstraintMaker* make) {
            make.centerY.equalTo(self.contentView);
            make.right.equalTo(self.contentView).mas_offset(-15);
        }];

        //SAM:iOS10兼容-cell   
        [self.contentView mas_remakeConstraints:^(MASConstraintMaker* make) {
            // iOS10 以上的适配
            if ([[[UIDevice currentDevice] systemVersion] floatValue] < 10.0) {
                make.left.top.right.equalTo(self);
                make.bottom.equalTo(self.ContentLabel.mas_bottom).mas_offset(15);
            }else{
                make.edges.equalTo(self);
            }
            
        }];
    }
}

#pragma mark - getter
- (UILabel*)titleLabel
{
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc] init];
    }
    return _titleLabel;
}

- (UILabel*)ContentLabel
{
    if (!_ContentLabel) {
        _ContentLabel = [[UILabel alloc] init];
    }
    return _ContentLabel;
}

- (UITextView*)contentTextView
{
    if (!_contentTextView) {
        _contentTextView = [[UITextView alloc] init];
        _contentTextView.delegate = self;
    }
    return _contentTextView;
}

- (UITextField*)contentField
{
    if (!_contentField) {
        _contentField = [[UITextField alloc] init];
    }
    return _contentField;
}

- (UISwitch*)switchContent
{
    if (!_switchContent) {
        _switchContent = [[UISwitch alloc] init];
        _switchContent.onTintColor = kColor_T;
    }
    return _switchContent;
}

- (NSIndexPath*)indexPath
{
    if (!_indexPath) {
        _indexPath = [[NSIndexPath alloc] init];
    }
    return _indexPath;
}
@end
